package MyWizard;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.netbeans.spi.wizard.Wizard;
import org.netbeans.spi.wizard.WizardPage;
import org.netbeans.spi.wizard.WizardPanelNavResult;
import preferences.Database.Column;
import preferences.Operator;

public class Page2 extends WizardPage implements ItemListener, ActionListener {

    private JCheckBox[] checkboxes;
    private JLabel[] labelsName;
    private JLabel[] labelsType;
    private JComboBox[] comboboxes;
    private JTextField[] textfields;
    private JPanel pnl;
    private JPanel pnl2;
    private JButton preview;
    private int x = 0;
    private boolean pre = false;

    public Page2() {
        init1();
    }

    private void init1() {
        if(WizardDemo.columns==null)
            WizardDemo.columns = WizardDemo.db.getColumns(WizardDemo.table);
        x = WizardDemo.columns.size();
        checkboxes = new JCheckBox[x];
        labelsName = new JLabel[x];
        labelsType = new JLabel[x];
        comboboxes = new JComboBox[x];
        textfields = new JTextField[x];
        pnl = new JPanel();
        pnl.setLayout(new GridLayout(0, 5));

        int i = 0;
        for (Column col : WizardDemo.columns) {
            checkboxes[i] = new JCheckBox();
            checkboxes[i].setMnemonic(i);
            checkboxes[i].addItemListener(this);
            labelsName[i] = new JLabel(col.getName());
            labelsType[i] = new JLabel("(" + col.getType() + ")");
            if(col.isNumeric())
                comboboxes[i] = new JComboBox(new String[]{"=", "<>", ">", ">=", "<", "<="});
            else
                comboboxes[i] = new JComboBox(new String[]{"=", "<>", ">", ">=", "<", "<=", "LIKE"});
            textfields[i] = new JTextField();
            if(col.getOp()!=null && col.getValue()!=null){
                textfields[i].setEnabled(true);
                comboboxes[i].setEnabled(true);
                checkboxes[i].setSelected(true);
                textfields[i].setText(col.getValue());
                comboboxes[i].setSelectedItem(col.getOp().toString());
            }else{
                textfields[i].setEnabled(false);
                comboboxes[i].setEnabled(false);
            }
            pnl.add(checkboxes[i]);
            pnl.add(labelsName[i]);
            pnl.add(labelsType[i]);
            pnl.add(comboboxes[i]);
            pnl.add(textfields[i]);
            i++;
        }
        preview = new JButton("Preview");
        preview.addActionListener(this);
        pnl.add(preview);
        add(pnl);
    }

    private void init2(List<String[]> results){
        pnl2=new JPanel(new GridLayout(0, x));
        JLabel[][] res = new JLabel[results.size()][x];
        int i=0,j=0;
        for(String[] tmp : results){
            j=0;
            for(String s:tmp){
                res[i][j]=new JLabel(s);
                pnl2.add(res[i][j]);
                j++;
            }
            i++;
        }
        pnl2.add(preview);
        add(pnl2);
    }


    @Override
    public WizardPanelNavResult allowNext(String stepName, Map settings, Wizard wizard) {
        return super.allowNext(stepName, settings, wizard);
    }

    private void getConstraints() {
        for (int i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].isEnabled() && textfields[i].getText().length() > 0) {
                WizardDemo.columns.get(i).setOpValue(Operator.getOperator(comboboxes[i].getSelectedItem().toString()),textfields[i].getText());
            }else{
                WizardDemo.columns.get(i).setOpValue(null,null);
            }
        }
    }

    public static final String getDescription() {
        return "Conditions";
    }

    @Override
    protected String validateContents(Component comp, Object o) {
        return null;
    }

    public void itemStateChanged(ItemEvent e) {
        int i = ((JCheckBox) e.getSource()).getMnemonic();
        if (checkboxes[i].isSelected()) {
            comboboxes[i].setEnabled(true);
            textfields[i].setEnabled(true);
        } else {
            comboboxes[i].setEnabled(false);
            textfields[i].setEnabled(false);
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(preview) && !pre) {
            pre = true;
            preview.setText("Resume");
            getConstraints();
            remove(pnl);
            init2(WizardDemo.db.getResults(WizardDemo.table,WizardDemo.getWhere()));
            repaint();
            revalidate();
        } else {
            pre = false;
            remove(pnl2);
            init1();
            repaint();
            revalidate();
        }
    }
}
